# Pat Leslie - December 2023

library(readr)
library(lubridate)
library(lme4)
library(MASS)
library(pscl)
library(stargazer)
library(margins)
library(tidyverse)



# data (set working directory to source)


JusticeLevel<-read_rds("JusticeLevel.rds")
justiceInterrupted<-read_rds("justiceInterrupted.rds")
advocateInterrupted<-read_rds("advocateInterrupted.rds")




# negative binomial interruptions 



summary(Of_Justice<-glm.nb(justiceCaseInterrupted~ 
                             #Case Characteristics
                             PanelSize+HoursArgument+
                             #Justice Characteristics
                             scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                             Experience+Female + specialisation_case_match +
                             #fixed effects omitted from table
                             Area  + ChiefJustice+
                             justiceCaseWords,
                           data=JusticeLevel))


summary(By_Justice<-glm.nb(justiceCaseInterruptor ~                              
                             #Case Characteristics
                             HoursArgument+
                             #Justice Characteristics
                             scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                             Experience+Female + specialisation_case_match +
                             #fixed effects omitted from table
                             Area  + ChiefJustice+
                             justiceCaseWords,
                           data=JusticeLevel))


summary(Of_Counsel<-glm.nb(JusticeInterruptsCounsel ~ 
                             #Case Characteristics
                             PanelSize+HoursArgument+
                             #Justice Characteristics
                             scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                             Experience+Female + specialisation_case_match +
                             #fixed effects omitted from table
                             Area  + ChiefJustice+
                             justiceCaseWords, 
                           data=JusticeLevel))



summary(Justice_Justice<-glm.nb(JusticeInterruptedByJustice ~ 
                                  #Case Characteristics
                                  PanelSize+HoursArgument+
                                  #Justice Characteristics
                                  scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                                  Experience+Female + specialisation_case_match +
                                  #fixed effects omitted from table
                                  Area  + ChiefJustice+
                                  justiceCaseWords, 
                                data=JusticeLevel))





stargazer(Of_Justice,By_Justice,Of_Counsel,Justice_Justice, type= 'html',
          style= 'ajps', keep.stat = c('n',"theta"),
          out = "Table1.html") 


# logistic interruptions



# differences in words per speech

justiceInterrupted$gender[justiceInterrupted$justiceGender==1]<-'Female'
justiceInterrupted$gender[justiceInterrupted$justiceGender==2]<-'Male'

justiceInterrupted$log_word_count<-log(justiceInterrupted$word_count+1)

justiceInterrupted$gender|>table()

t.test(word_count~gender, data=justiceInterrupted)




# model 1 of justice  


summary(m1_ofJ<-glm(Interrupted ~ 
                            #Case Characteristics
                            PanelSize+HoursArgument+
                            #Justice Characteristics
                            scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                            Experience+as.factor(Female) + specialisation_case_match +
                            #volubility controls
                            word_count + words_cumsum+
                            #fixed effects omitted from table
                            ChiefJustice+ Area
                          , 
                          data = justiceInterrupted , family='binomial'))


 
# model 2 by justice

summary(m2_byJ<-glm(Interruptor ~  
                            #Case Characteristics
                            PanelSize+HoursArgument+
                            #Justice Characteristics
                            scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                            Experience+as.factor(Female) + specialisation_case_match +
                            #volubility controls
                            lag_word_count + lag_words_cumsum+
                            #fixed effects ommitted from table
                            ChiefJustice+ Area
                          , 
                          data = justiceInterrupted , family='binomial'))



# model 3 justice interrupts counsel 


summary(m3_ofC<-glm(JusticeInterruptsCounsel ~  
                                     #Case Characteristics
                                     PanelSize+HoursArgument+
                                     #Justice Characteristics
                                     scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                                     Experience+as.factor(Female) + specialisation_case_match +
                                     #volubility controls
                                     lag_word_count + lag_words_cumsum+
                                     #fixed effects ommitted from table
                                     ChiefJustice+ Area
                                   , 
                                   data = justiceInterrupted , family='binomial'))






# model 4 justices interrupted by justices  

summary(m4_ofJ<-glm(JusticeInterruptedByJustice ~ 
                                     #Case Characteristics
                                     PanelSize+HoursArgument+
                                     #Justice Characteristics
                                     scIdeolScore +scIdeolScore:ALP_CJ+IsChiefJustice+
                                     Experience+as.factor(Female) + specialisation_case_match +
                                     #volubility controls
                                     word_count + words_cumsum+
                                     #fixed effects ommitted from table
                                     ChiefJustice+ Area
                                   , 
                                   data = justiceInterrupted , family='binomial'))

 
# model 5 of counsel  

summary(m5_ofC<-glm(Interrupted ~ 
                            female_attorney+ attorney_seniority+ 
                            #case characteristics
                            PanelSize+HoursArgument+
                            #volubility controls
                            word_count + words_cumsum+
                            #fixed effects omitted from table
                            ChiefJustice +Area, 
                          data = advocateInterrupted, family='binomial'))


# logit output - table

stargazer::stargazer(m1_ofJ,
                     m2_byJ,
                     m3_ofC,
                     m4_ofJ, 
                     m5_ofC,
                     type = "html", 
                     out = "Table2.html")


# summarising marginal effects, uncomment to run (takes some time)


# m1mfx<-margins(m1_ofJ)
# m2mfx<-margins(m2_byJ)
# m3mfx<-margins(m3_ofC)
# m4mfx<-margins(m4_ofJ)
# m5mfx<-margins(m5_ofC)

# summary(m1mfx)
# summary(m2mfx)
# summary(m3mfx)
# summary(m4mfx)
# summary(m5mfx)


